package com.example.cloudcashierpos.http;


import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.cloudcashierpos.bean.MsgNet;
import com.example.cloudcashierpos.net.BaseApplication;


import java.net.SocketTimeoutException;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AccessNet {
    // 初始化BaseAPPlication里面的Retrofit，并将RegisterSerivce里面的url和参数传入，拿到的数据
    private static RegisterSerivce registerSerivce = BaseApplication.getInstance().getRetrofit().create(RegisterSerivce.class);


    public static boolean postDataNet(final Handler mHandler, final String url, final int urlno, String datainfo) {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, datainfo); // datainfo json传递请求参数
        registerSerivce.postDataNet(url, body).enqueue(new Callback<MsgNet>() {
            @Override
            public void onResponse(Call<MsgNet> call, Response<MsgNet> response) {
                //  boolean ispassed=false;
                Message message = mHandler.obtainMessage();
                message.arg2 = urlno;
                message.what = 9901;
                if (response.isSuccessful()) {
//                    Log.d("postDataNet成功响应", response.body().toString());
                    // 响应成功
//                    if (response.body().getStatus() == 0) {
////                        Log.d("postDataNet成功返回数据", response.body().toString());
//                        // 数据为0，后台正确响应
//                        message.what = 9900;
//                        message.obj = response.body().getData();
//                    } else {
//                        // 数据不为0，后台错误响应
//                        Log.d("postDataNet错误返回数据", response.body().toString());
//                        message.what = 9901;
//                        message.obj = response.body().getMsg();
//                    }
                } else {
                    // 超时
                    message.what = 9901;
                    message.obj = "请求失败，请稍后重试";
                }
                mHandler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<MsgNet> call, Throwable t) {
                Log.d("postDataNet错误返回数据onFailure", "xxxxx");
                Message message = mHandler.obtainMessage();
                message.arg2 = urlno;
                message.what = 9902;
                try {
                    if(t instanceof SocketTimeoutException) {
                        message.what = 9901;
                        message.obj = "请求超时，请重试";
                    } else {
                        if (t.getMessage() == null) {
                            message.obj = t.getClass().toString();
                        } else {
                            message.obj = t.getMessage();
                        }
                    }

                } catch (Exception ex) {
                    message.obj = ex.getMessage();
                }
                mHandler.sendMessage(message);
            }
        });
        return true;
    }


}
